home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 52811 / 52811.xpi / chrome / content / AES / aesprng.js < prev    next >
Encoding:
JavaScript  |  2003-11-27  |  2.4 KB  |  98 lines

  1.  
  2.     //  AES based pseudorandom number generator
  3.  
  4.     /*  Constructor.  Called with an array of 32 byte (0-255) values
  5.     containing the initial seed.  */
  6.  
  7.     function AESprng(seed) {
  8.     this.key = new Array();
  9.     this.key = seed;
  10.     this.itext = hexToByteArray("9F489613248148F9C27945C6AE62EECA3E3367BB14064E4E6DC67A9F28AB3BD1");
  11.     this.nbytes = 0;            // Bytes left in buffer
  12.     
  13.     this.next = AESprng_next;
  14.     this.nextbits = AESprng_nextbits;
  15.     this.nextInt = AESprng_nextInt;
  16.     this.round = AESprng_round;
  17.     
  18.     /*  Encrypt the initial text with the seed key
  19.         three times, feeding the output of the encryption
  20.         back into the key for the next round.  */
  21.     
  22.     bsb = blockSizeInBits;
  23.     blockSizeInBits = 256;    
  24.     var i, ct;
  25.         for (i = 0; i < 3; i++) {
  26.         this.key = rijndaelEncrypt(this.itext, this.key, "ECB");
  27.     }
  28.     
  29.     /*  Now make between one and four additional
  30.         key-feedback rounds, with the number determined
  31.         by bits from the result of the first three
  32.         rounds.  */
  33.     
  34.     var n = 1 + (this.key[3] & 2) + (this.key[9] & 1);    
  35.         for (i = 0; i < n; i++) {
  36.         this.key = rijndaelEncrypt(this.itext, this.key, "ECB");
  37.     }
  38.         blockSizeInBits = bsb;
  39.     }
  40.     
  41.     function AESprng_round() {
  42.     bsb = blockSizeInBits;
  43.     blockSizeInBits = 256;    
  44.         this.key = rijndaelEncrypt(this.itext, this.key, "ECB");
  45.     this.nbytes = 32;
  46.         blockSizeInBits = bsb;
  47.     }
  48.     
  49.     //    Return next byte from the generator
  50.  
  51.     function AESprng_next() {
  52.         if (this.nbytes <= 0) {
  53.         this.round();
  54.     }
  55.     return(this.key[--this.nbytes]);
  56.     }
  57.     
  58.     //    Return n bit integer value (up to maximum integer size)
  59.     
  60.     function AESprng_nextbits(n) {
  61.         var i, w = 0, nbytes = Math.floor((n + 7) / 8);
  62.  
  63.     for (i = 0; i < nbytes; i++) {
  64.         w = (w << 8) | this.next();
  65.     }
  66.     return w & ((1 << n) - 1);
  67.     }
  68.  
  69.     //  Return integer between 0 and n inclusive
  70.     
  71.     function AESprng_nextInt(n) {
  72.         var p = 1, nb = 0;
  73.     
  74.     //  Determine smallest p,  2^p > n
  75.     //  nb = log_2 p
  76.     
  77.     while (n >= p) {
  78.         p <<= 1;
  79.         nb++;
  80.     }
  81.     p--;
  82.     
  83.     /*  Generate values from 0 through n by first generating
  84.         values v from 0 to (2^p)-1, then discarding any results v > n.
  85.         For the rationale behind this (and why taking
  86.         values mod (n + 1) is biased toward smaller values, see
  87.         Ferguson and Schneier, "Practical Cryptography",
  88.         ISBN 0-471-22357-3, section 10.8).  */
  89.  
  90.     while (true) {
  91.             var v = this.nextbits(nb) & p;
  92.         
  93.         if (v <= n) {
  94.             return v;
  95.         }
  96.     }
  97.     }
  98.